/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var other_file = 0;
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function (_args) {
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agents/apply',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {
                        '.item-agree:click': 'agree',
                        '.item-reject:click': 'reject',
                        '.item-memo:click': 'memo',
                    },
                    agree: function (e) {
                        e.preventDefault();
                        var data = {};
                        data.id = this.get_data('id');
                        data.status = 1;
                        dialog({
                            title: '同意-审核代理商',
                            content: '登录账号：手机号<br />登录密码：1234567',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'agent/apply/status',
                                        data: data
                                    }).put()) {
                                    _this.collection.fetch();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    reject: function (e) {
                        e.preventDefault();
                        var data = {};
                        data.id = this.get_data('id');
                        data.status = 2;
                        dialog({
                            content: '确定要拒绝此申请吗？',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'agent/apply/status',
                                        data: data
                                    }).put()) {
                                    _this.collection.fetch();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    memo: function(e) {
                        e.preventDefault();
                        var data = {};
                        data.agent_id = this.get_data('agent_id');
                        data.agency_id = this.get_data('agency_id');
                        dialog({
                            content: template('memo-tpl'),
                            ok: function () {
                                data.memo =  $('#memo').val();
                                console.info(data, 'data----->');

                                if (new Model({
                                        url: _global.url.api + 'agent/apply/note',
                                        data: data
                                    }).put()) {
                                    _this.collection.fetch();
                                    return true;
                                }
                                return false;
                            },cancel: function() {}
                        }).show(e.target);
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});